home *** CD-ROM | disk | FTP | other *** search
- Path: easy.in-chemnitz.de!mkmk!floh
- From: floh@mkmk.in-chemnitz.de (Andre Weissflog)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: manipulating the stack in C
- Message-ID: <Sh++x*aE0@mkmk.in-chemnitz.de>
- Date: Fri, 26 Jan 1996 00:42:30 CET
- Reply-To: floh@mkmk.in-chemnitz.de
- References: <4e8b20$lsq@newshost.lanl.gov>
- Distribution: world
- Organization: private uucp site
- X-Newsreader: Arn V 1.04
-
- In article <4e8b20$lsq@newshost.lanl.gov>, Luke Emmert writes:
-
- > I'm currently writing an assembly function that takes a variable
- > number of arguments. The last of the arguments is indicated by forcing
- > the stack to point at itself(a low probability occurence so I hope).
- > The function is called from C code and I therefore need to get C to do
- > something weird with the stack. Unfortunately, I need to make
- > assumptions about how the compiler will produce binary.
- >
- > The code looks like the following:
- >
- > #define LAST_ARG (get_stack())
- >
- > foo( arg1, arg2, arg3, arg4, LAST_ARG );
- >
- I think it would be nicer if you give foo() the number of
- args first, following the args:
-
- void foo(ULONG num_args,...);
-
- Since C pushes the last arg first, you will find num_args
- always at 4(a7), followed by the "real" arguments starting
- at 8(a7):
-
- XDEF foo
- foo lea 4(a7),a0
- movem.l d2-d7,-(a7)
- move.l (a0),d0 ; get <num_args>
- ...
-
- > where get_stack() simply returns the current
- > stack value:
- >
- > _get_stack:
- > move.l a7,d0
- > rts
- >
- Don't forget that the return address is also on the stack, your
- _get_stack routine will return the stack pointer 4 bytes too low
- (since rts pops 4 bytes).
-
- > The problem arose last night, when I removed a printf() statement
- > that I used for debugging. After disassembling the binary I found that
- > the stack was loaded in different ways in each case. My program
- > assumes a particular way of loading the stack and that's why it failed.
- >
- > What I'd like to do is have something in-line that loads the current
- > stack value rather like
- > move.l a7,(a7)
-
- Hummm? Whazzat?
-
- You don't need to adjust the stack pointer yourself, just be sure
- to leave the routine with the same stack pointer as it was entered.
- The adjustment for the routine's arguments that where pushed onto
- the stack will be done on the callers level and is managed
- by the compiler.
-
- If you have a varargs routine and call it like
-
- foo(1,2,3,4);
-
- The compiler will generate something like:
-
- pea 4 ; push values onto stack
- pea 3
- pea 2
- pea 1
- bsr _foo ; call foo()
- addq.w #16,a7 ; re-adjust stack
-
- > without having to use a function call. I'm currently using SAS C v5.10
- > and I'm pretty sure that this cannot be done, but if any one has a
- > suggestion I would appreciate it.
- >
-
- You could still use getreg(REQ_A7), but I wouldn't necessarly
- count on the returned value still beeing valid when you need it.
-
- Bye,
- -Floh.
-
- ====//=== Andre Weissflog <floh@mkmk.in-chemnitz.de> =======
- ...// Sep'95: Return Of The Living Death...................
- \\// 90% of everything is crap (Sturgeon's Law)...........
- =\\===============================================Amiga!=
-
-